package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.format.annotation.DateTimeFormat;
import java.time.LocalDate;
import java.util.List;

/**
 * 员工管理Controller
 */
@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;

    /**
     * 无条件分页查询方法1（不使用插件）       注意@RequestParam设置默认值
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Short gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){


        log.info("分页查询，参数：{},{},{},{},{},{}",page,pageSize,name,gender,begin,end);

        System.out.println(begin);
        System.out.println(end);

        PageBean pageBean=empService.page(page,pageSize,name,gender,begin,end);
        return Result.success(pageBean);
    }
//批量删除员工功能
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){

        log.info("删除员工组,ids:{}",ids);
//利用foreach技术遍历
        for(Integer a: ids)
        {
            System.out.println(a);
        }

        empService.delete(ids);
        return Result.success();
    }
    //新增员工功能
    @PostMapping
    public Result addEmp(@RequestBody Emp emp){
        log.info("新增员工，emp：{}",emp);

        empService.addEmp(emp);
        return Result.success();
    }
    //根据id查询员工信息
    @GetMapping("/{id}")
    public Result selectId(@PathVariable Integer id){
        log.info("根据id查询员工，id为：{}",id);

        Emp emp = empService.selectId(id);
        return Result.success(emp);
    }
    //更新员工
    @PutMapping
    public Result upEmp(@RequestBody Emp emp){
        log.info("更新员工信息",emp);

        empService.updata(emp);
        return Result.success();
    }
}
